﻿using System;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRevitDimension.DetailIndex.Jig;
using YJKRevitDimension.SymbolTextSetting;
using YJKGeometryFuncs;

namespace YJKRevitDimension.DetailIndex.Operate
{
	internal class MultiSliceDetailIndexOperate : DetailIndexOperateBase, IDetailIndexOperate
	{
		public bool Finished { get; set; }

		public MultiSliceDetailIndexOperate(ExternalCommandData revit, DetailIndexParameter detailIndexPara) : base(revit, detailIndexPara)
		{
			this.Finished = false;
		}

		public Result Operate()
		{
			base.TempElementIds.Clear();
			Transaction transaction = new Transaction(this.m_Doc, "Create TempElement");
			DetailIndexJig detailIndexJig = new DetailIndexJig(this.m_UIDoc, base.IfUseCustomAngle, this.Angle);
			Result result;
			try
			{
				transaction.Start();
				XYZ xyz = detailIndexJig.PickStartPoint("请选择第一点");
				base.m_DetailIndexParameter.StartPoint = xyz;
				XYZ xyz2 = detailIndexJig.PickElbowPoint(xyz, "请选择转折点");
				base.m_DetailIndexParameter.ElbowPoint = xyz2;
				base.TempElementIds.Add(this.m_Doc.Create.NewDetailCurve(this.m_Doc.ActiveView, Line.CreateBound(xyz, xyz2)).Id);
				XYZ xyz3 = detailIndexJig.PickEndPoint(xyz2, "请选择标注终点");
				base.m_DetailIndexParameter.EndPoint = xyz3;
				XYZ xyz4;
				if (base.GetForward(xyz2, xyz3))
				{
					xyz4 = xyz3.Add(this.m_Doc.ActiveView.RightDirection * DetailIndexOperateBase.m_Radis * (double)this.m_Doc.ActiveView.Scale);
				}
				else
				{
					xyz4 = xyz3.Add(-this.m_Doc.ActiveView.RightDirection * DetailIndexOperateBase.m_Radis * (double)this.m_Doc.ActiveView.Scale);
				}
				Arc arc = Arc.Create(xyz4, DetailIndexOperateBase.m_Radis * (double)this.m_Doc.ActiveView.Scale, 0.0, Math.PI*2, this.m_Doc.ActiveView.RightDirection, this.m_Doc.ActiveView.UpDirection);
				base.TempElementIds.Add(this.m_Doc.Create.NewDetailCurve(this.m_Doc.ActiveView, arc).Id);
				base.TempElementIds.Add(this.m_Doc.Create.NewDetailCurve(this.m_Doc.ActiveView, Line.CreateBound(xyz2, xyz3)).Id);
				detailIndexJig.Dispose();
				XYZ slicePoint = this.m_Selection.PickPoint("请选择指示剖切符号方向的点");
				base.m_DetailIndexParameter.SlicePoint = slicePoint;
				base.m_DetailIndexParameter.SliceVector = base.GetSliceNormal(xyz, xyz2, slicePoint);
				base.m_DetailIndexParameter.LstSlicePoints.Clear();
				for (;;)
				{
					try
					{
						XYZ xyz5 = this.m_Selection.PickPoint("请选择剖切符号插入点，ESC结束选择");
						xyz5 = Geometry.CalculateFootPoint(xyz, xyz2, xyz5);
						base.m_DetailIndexParameter.LstSlicePoints.Add(xyz5);
						ElementId elementId = null;
						double offset = xyz5.DistanceTo(xyz2) / this.m_ViewScale;
						base.CreateFilledRegion(xyz2, offset, this.m_Doc, this.m_Doc.ActiveView, ref elementId, (double)this.m_Doc.ActiveView.Scale, true);
						this.m_Doc.Regenerate();
						if (elementId != null)
						{
							base.TempElementIds.Add(elementId);
						}
						continue;
					}
					catch (Exception)
					{
					}
					break;
				}
				base.m_DetailIndexParameter.IsForward = base.GetForward(xyz2, xyz3);
				base.m_DetailIndexParameter.BaseLineLength = xyz3.DistanceTo(xyz2) / (double)this.m_Doc.ActiveView.Scale;
				transaction.Commit();
				this.Finished = true;
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception)
			{
				detailIndexJig.Dispose();
				transaction.RollBack();
				base.DeleteTempElement();
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		public bool Create(DetailIndexParameter detailIndexPara)
		{
			base.UpdateDetailIndexParameter(detailIndexPara);
			if (!this.EditFamily())
			{
				base.DeleteTempElement();
				return false;
			}
			if (!base.CreateFamilySymbol())
			{
				base.DeleteTempElement();
				return false;
			}
			return base.DeleteTempElement();
		}

		public bool EditFamily()
		{
			XYZ xyz = (base.m_DetailIndexParameter.StartPoint - base.m_DetailIndexParameter.ElbowPoint) / this.m_ViewScale;
			XYZ zero = XYZ.Zero;
			string text = xyz.DistanceTo(zero).ToString("f5");
			string text2 = base.m_DetailIndexParameter.StartPoint.DistanceTo(base.m_DetailIndexParameter.ElbowPoint).ToString("f5");
			Document document = this.m_Revit.Application.Application.OpenDocumentFile(this.m_SourceFamilyDocPath);
			Autodesk.Revit.DB.View view = new FilteredElementCollector(document).OfClass(typeof(Autodesk.Revit.DB.View)).First<Element>() as Autodesk.Revit.DB.View;
			Transaction transaction = new Transaction(document, "Create Index Symbol");
            if ((int)transaction.Start() != 1)
			{
				this.m_TargetFamilyDocPath = "";
				return false;
			}
			try
			{
				ElementId elementId = null;
				double offset = base.m_DetailIndexParameter.StartPoint.DistanceTo(base.m_DetailIndexParameter.ElbowPoint) / this.m_ViewScale;
				base.CreateFilledRegion(base.m_DetailIndexParameter.ElbowPoint, offset, document, view, ref elementId, 1.0, false);
				foreach (XYZ xyz2 in base.m_DetailIndexParameter.LstSlicePoints)
				{
					offset = xyz2.DistanceTo(base.m_DetailIndexParameter.ElbowPoint) / this.m_ViewScale;
					base.CreateFilledRegion(base.m_DetailIndexParameter.ElbowPoint, offset, document, view, ref elementId, 1.0, false);
				}
				this.m_TargetFamilyDocPath = string.Concat(new string[]
				{
					Ultility.GetUserTempDirectory(),
					"NoRange -",
					text,
					"-",
					text2,
					"-HY -IndexMark.rfa"
				});
				transaction.Commit();
				SaveAsOptions saveAsOptions = new SaveAsOptions();
				saveAsOptions.OverwriteExistingFile = true;
				document.SaveAs(this.m_TargetFamilyDocPath, saveAsOptions);
			}
			catch (Exception)
			{
				this.m_TargetFamilyDocPath = "";
				transaction.RollBack();
				return false;
			}
			return true;
		}
	}
}
